//
//  BDHUD.m
//  ProjectForBD
//
//  Created by Jaesun on 16/12/22.
//  Copyright © 2016年 B.D. All rights reserved.
//

#import "BDHUD.h"
#import "NoneDateView.h"
#import "RZCustomLable.h"
static BDHUD *hud = nil;

@interface BDHUD()

@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) MBProgressHUD *progressHUD;
@property (nonatomic, strong) UIView *overlayView;

@end

@implementation BDHUD

+ (instancetype)shareManager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hud = [[BDHUD alloc] init];
    });
    return hud;
}

+ (instancetype)hud {
    
    BDHUD *hud = [[BDHUD alloc] init];
    return hud;
}

- (void)dismiss {
    [self.view removeFromSuperview];
}

/**
 *  提示文本
 */
- (void)showWords:(NSString*)title {
    if([Toolobject IsEmptyString:title]){
        title = @"未知信息";
    }
   
    [APPDELEGATE_KEY_WINDOW makeToast:title
                           duration:CLEAR_TIME
                           position:[NSValue valueWithCGPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/2.f, [UIScreen mainScreen].bounds.size.height /2.f - 50)]];
}

/**
 *  提示文本
 */
- (void)showLongTimeWords:(NSString*)title {
    
    if([Toolobject IsEmptyString:title]){
        title = @"未知信息";
    }
//    [UIApplication sharedApplication].keyWindow
    [APPDELEGATE_KEY_WINDOW makeToast:title
                             duration:CLEAR_TIME * 4
                             position:[NSValue valueWithCGPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/2.f, [UIScreen mainScreen].bounds.size.height /2.f - 50)]];
    
}

/**
 *  无数据提示
 */
- (void)showImage:(UIImage *)image toView:(UIView*)superView taped:(BDImageViewTap)tapBlock {
    
    [self dismiss];
    
    if(![superView isKindOfClass:[UIView class]]){
        self.view = APPDELEGATE_KEY_WINDOW;
        self.view.backgroundColor = [UIColor clearColor];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMidX(superView.bounds) - image.size.width / 2, CGRectGetMidY(superView.bounds) - image.size.height / 2 - 30, image.size.width, image.size.height)];
    imageView.image = image;
    imageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *gesutre = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTapAction:)];
    [imageView addGestureRecognizer:gesutre];
    self.view = imageView;
    
    [superView addSubview:self.view];
    self.tapBlock = tapBlock;
    
}

- (void)imageViewTapAction:(UITapGestureRecognizer *)sender {
   
    if (self.tapBlock) {
       self.tapBlock();
    }
}

/**
 *  无数据提示
 */
-(void)showNOneDataViewHUD:(UIView*)superView  top:(CGFloat)topy title:(NSString*)title onClick:(void (^)(void))click{
    [self dismiss];
    if(![superView isKindOfClass:[UIView class]]){
        superView=[UIApplication sharedApplication].keyWindow;
        
    }
    if(![title isKindOfClass:[NSString class]]){
        title=[THMangerTool language:@"HUD.title"];
    }
    self.overlayView=superView;
    
    UIImage *img= [UIImage imageNamed:@"暂无记录"];
    
    NoneDateView *_noneDateView=[[NoneDateView alloc] initView:self.overlayView setImage:img setImageFream:CGRectMake((self.overlayView.frame.size.width- img.size.width)/2, topy, img.size.width, img.size.height) Title:title];
    
    _noneDateView.tag=20160618;
    
    _noneDateView.selectBlock=^(NSDictionary *dict){
        if(click){
            click();
        }
    };
    for(RZCustomLable *lab in _noneDateView.subviews){
        if([lab isKindOfClass:[RZCustomLable class]]){
            lab.font=CustomFont(13);
            lab.top+=10;
            lab.width=BOUNDSWIDTH-20;
            lab.left=10;
            
        }
    }
    
}

-(void)dismissHUD{
    BOOL isOK=YES;
    while (isOK) {
        
        MBProgressHUD *view=(MBProgressHUD*)[self.overlayView viewWithTag:20160618];
        if(view){
            [view removeFromSuperview ];
        }
        else{
            isOK=NO;
        }
    }
    
    
}

- (MBProgressHUD *) showHUDAddedTo:(UIView *)view title:(NSString *)title animated:(BOOL)animated {
   
    if (self.progressHUD) {
        [self.progressHUD hideAnimated:NO];
    }

    self.progressHUD  = [[MBProgressHUD alloc] initWithView:view];
    if (![Toolobject IsEmptyString:title]) {
        self.progressHUD.label.text = title;
    }
    self.progressHUD.removeFromSuperViewOnHide = YES;
    [view addSubview:self.progressHUD];
    [self.progressHUD showAnimated:animated];
    
    return self.progressHUD;
}

- (void)hideAnimated:(BOOL)animated {
    
    [self.progressHUD hideAnimated:animated];
}

- (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated {
    MBProgressHUD *hud = [self HUDForView:view];
    if (hud != nil) {
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:animated];
        return YES;
    }
    return NO;
}

- (MBProgressHUD *)HUDForView:(UIView *)view {
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:[MBProgressHUD class]]) {
            return (MBProgressHUD *)subview;
        }
    }
    return nil;
}

@end
